package com.leetcode.p0;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * 时间 复杂度 n^2
 */
public class Test46 {

    private final List<List<Integer>> res = new ArrayList<>();

    private Deque<Integer> path;

    private boolean[] used;

    private int length = 0;

    public List<List<Integer>> permute(int[] nums) {
        length = nums.length;
        if (length <= 0) {
            return res;
        }
        used = new boolean[length];
        path = new ArrayDeque<>(length);

        dfs(nums, 0);
        return res;
    }

    private void dfs(int[] nums, int begin) {
        if (begin == length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < length; i++) {
            if (!used[i]) {
                used[i] = true;
                path.addLast(nums[i]);

                System.out.println("传值前  " + path);

                dfs(nums, begin + 1);
                path.removeLast();

                System.out.println("传值后  " + path);

                used[i] = false;
            }
        }
    }

    public static void main(String[] args) {
        Test46 test = new Test46();
        System.out.println(test.permute(new int[]{1, 2, 3}));
    }

}
